﻿package com.cloneoner.test.slider
{
	import fr.digitas.flowearth.debug.FPS;
	import flash.display.Graphics;
	import fr.digitas.flowearth.core.IIterator;
	import com.cloneoner.test.slider.data.ItemData;
	import com.cloneoner.test.slider.view.ItemImg;
	import com.cloneoner.test.slider.view.SliderBmpdBlur;
	import fr.digitas.flowearth.conf.Conf;

	import com.cloneoner.test.slider.data.DataProvider;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLRequest;


	public class Main extends Sprite
	{


		// __________________________________________________________
		// -										 	  CONSTRUCTOR
		public function Main()
		{
			if ( stage)
				_init();
			else
				addEventListener(Event.ADDED_TO_STAGE, _init);
		}
		
		
		// __________________________________________________________
		// -										 PRIVATES METHODS
		private function _init(e : Event = null) : void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			_loadConf();
		}
		
		private function _loadConf() : void
		{
			Conf.addEventListener(Event.COMPLETE, _onConfLoaded);
			
			var urlXML : String = loaderInfo.parameters.manifest || "conf.xml";
			Conf.loadXml(new URLRequest(urlXML));
		}
		
		private function _onConfLoaded(e : Event) : void
		{
			Conf.removeEventListener(Event.COMPLETE, _onConfLoaded);

			var data : DataProvider = new DataProvider();

			const widthSlider : int = Conf.getNumber("width_slider");
			const heightSlider : int = Conf.getNumber("height_slider");
			const gap : int = Conf.getNumber("gap");
			
			var item:ItemImg;
			var itData:ItemData;
			var it:IIterator = data.getImgs();
			
			var slider:SliderBmpdBlur = new SliderBmpdBlur(widthSlider, heightSlider, gap);
			
			while( itData = it.next() as ItemData)
			{
				item = new ItemImg(itData);
				slider.addItem(item);
			}
			
			slider.y = 200;
			
			addChild(slider);
			slider.load();
			
			addChild(new FPS());
			
			stage.addEventListener(Event.RESIZE, _onResize);	
		}

		private function _onResize(event : Event) : void
		{
			var g:Graphics = this.graphics;
			
			g.clear();
			g.beginFill(0, .1);
			g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			g.endFill();
		}
	}
}